#include "flameshotdbusadapter.h"
//#include "src/utils/confighandler.h"
#include "../utils/screengrabber.h"
#include "controller.h"
#include "../utils/screencapsaver.h"
#include "../utils/systemnotification.h"
#include <QBuffer>
#include <QGSettings/qgsettings.h>
#include <QDebug>

FlameshotDBusAdapter::FlameshotDBusAdapter(QObject *parent) :
    QDBusAbstractAdaptor(parent)
{
    auto controller = Controller::getInstance();
    connect(controller, &Controller::captureFailed,
            this, &FlameshotDBusAdapter::captureFailed);
//    connect(controller, &Controller::captureTaken,
//            this, &FlameshotDBusAdapter::handleCaptureTaken,Qt::QueuedConnection);
    connect(controller, &Controller::captureTaken,
            this, &FlameshotDBusAdapter::captureTaken);

    connect(controller, &Controller::captureExit,
            this, &FlameshotDBusAdapter::captureExit);
}

FlameshotDBusAdapter::~FlameshotDBusAdapter()
{
}

void FlameshotDBusAdapter::graphicCapture(uint id)
{
    CaptureRequest req(CaptureRequest::GRAPHICAL_MODE);
    req.setStaticID(id);
    qDebug()<<"FlameshotDBusAdapter::graphicCapture 录屏GUI mode 和id "<< req.captureMode() <<req.id();
    Controller::getInstance()->requestCapture(req);
}

void FlameshotDBusAdapter::osManagerCapture(uint id)
{
    CaptureRequest req(CaptureRequest::OSMANAGER_MODE);
    req.setStaticID(id);
    qDebug()<<"FlameshotDBusAdapter::osManagerCapture 管家录制 mode 和id "<< req.captureMode() <<req.id();
    Controller::getInstance()->requestCapture(req);
}

void FlameshotDBusAdapter::fullScreen(uint id)
{
    CaptureRequest req(CaptureRequest::FULLSCREEN_MODE);
    req.setStaticID(id);
    qDebug()<<"FlameshotDBusAdapter::fullScreen 全屏录制 mode 和id "<< req.captureMode() <<req.id();
    Controller::getInstance()->requestCapture(req);
}

void FlameshotDBusAdapter::openSetting()
{
    qDebug()<<"FlameshotDBusAdapter::openSetting 打开设置";
    Controller::getInstance()->openSettingWid();
}


void FlameshotDBusAdapter::captureScreen(int number, uint id)
{

    CaptureRequest req(CaptureRequest::SCREEN_MODE, number);
    req.setStaticID(id);
    qDebug()<<"FlameshotDBusAdapter::captureScreen 窗口录制 mode 和id "<< req.captureMode() <<req.id();
    Controller::getInstance()->requestCapture(req);
}

void FlameshotDBusAdapter::trayIconEnabled(bool enabled)
{
    auto controller = Controller::getInstance();
    if (enabled) {
        controller->enableTrayIcon();
    } else {
        controller->disableTrayIcon();
    }
}

/*void FlameshotDBusAdapter::handleCaptureTaken(uint id, const QPixmap &p)
{
    qDebug()<<__LINE__<<__func__<<"FlameshotDBusAdapter"<<"invoke begin ";
    QByteArray byteArray;
    QBuffer buffer(&byteArray);
    p.save(&buffer, "PNG");
    emit captureTaken(id, byteArray);
}
*/
